#include "Montion.h"
#include "Wstring.h"

Montion::Montion(IArduino& arduino, MessageRouter& router, const String& name, ILogger& logger, int pin, long milisSignalPeriod): 
Service(arduino, router, name, logger), milisSignalPeriod_(milisSignalPeriod), lastSignalSent_(0), pin_(pin) {
	arduino_.pin(pin_, arduino_.IN());
	arduino_.dWrite(pin_, arduino_.HIGHVAL());
}

void Montion::tick(long milliseconds) {
	int value = arduino_.dRead(pin_);
	if(value == arduino_.HIGHVAL() && milliseconds - lastSignalSent_ > milisSignalPeriod_){
		postMessage(String(F("{\"event\":\"montion\", \"id\":\"")) + getName() + F("\"}"));
		lastSignalSent_ = milliseconds;
	}
}